﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FantastPlatform.Decision.IService;
using FantastPlatform.Framework;
using FantastPlatform.Framework.Core;
using System.Configuration;
using System.Data.SqlClient;
using FantastPlatform.Decision.IModel;
using FantastPlatform.Framework.IModel;

namespace FantastPlatform.Decision.UI.Service
{
    public partial class PatrolTaskPointService : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int TotalCount = 0;
            IPatrolTaskService Service = ServiceFactory.CreateService<IPatrolTaskService>();
            int UnitId = GetUnitIdByPoliceCode(LoginEmployee.Code);
            switch (Action)
            {
                case "Get":
                    IPatrolTask task = Service.Get(Id);
                    IPatrolTask orgTask = task.Parent;
                    IPatrolTask allTask = task.Parent.Parent;
                    object taskobj = (new
                    {
                        TaskName = allTask.Name,
                        DefaultLeader = allTask.DefaultLeader.Name,
                        TaskType = allTask.TaskType.Name,
                        DutyOrganization = allTask.DutyOrganization.Name,
                        DutyLeader = allTask.DutyLeader.Name,
                        BeginTime = orgTask.BeginTime,
                        EndTime = orgTask.EndTime,
                        ExecOrganization = orgTask.ExecOrganization.Name,
                        SelfOrgCar = task.SelfOrgCar,
                        PositionName = task.Position == null ? "" : task.Position[0].Name,
                        Require = orgTask.Require,
                        LeaderName = orgTask.DefaultLeader.Name,
                        TelPhone = GetPhone(orgTask.DefaultLeader),
                        Points = task.Position == null ? "" : GetPoints(task.Position[0].GPSPoints),
                        SelfOrgPersonNum = task.SelfOrgPersonNum,
                        SelfOrgPersons = task.Policemans == null ? "" : GetPersons(task.Policemans),
                        OtherOrgName = task.OtherOrgName,
                        OtherOrgPersonNum = task.OtherOrgPersonNum,
                        OtherOrgPersons = task.OtherOrgPerson
                    });
                    ReturnObj2(taskobj);
                    break;
                case "Search":
                    IList<IPatrolTask> patrolTaskList = Service.SearchPointTaskByUnitNameAndTime(UnitId, null, this.PageSize, this.SelfPage, out TotalCount);
                    if (patrolTaskList.Count > 0)
                    {
                        var objs = (from p in patrolTaskList
                                    select new
                                    {
                                        Id = p.Id,
                                        TaskName = p.Parent.Parent.Name,
                                        PointName = p.Name,
                                        TaskType = p.Parent.Parent.TaskType.Name,
                                        BeginTime = p.Parent.BeginTime.ToString().Replace('/', '-'),
                                        EndTime = p.Parent.EndTime.ToString().Replace('/', '-'),
                                        DutyOrganization = p.Parent.Parent.DutyOrganization.Name,
                                        ExecOrganization = p.Parent.ExecOrganization.Name,
                                        PointLeader = p.Parent.DefaultLeader.Name,
                                        TelPhone = GetPhone(p.Parent.DefaultLeader),
                                        PersonNum = p.SelfOrgPersonNum
                                    }).ToArray();
                        ReturnList(objs.Length, objs);
                    }
                    break;
            }
        }

        private string GetPersons(IList<IEmployee> iList)
        {
            string persons = string.Empty;
            for (int i = 0; i < iList.Count; i++)
            {
                if (i == 0)
                    persons += iList[i].Name;
                else
                    persons += "," + iList[i].Name;
            }
            return persons;
        }

        private string GetPoints(IList<IGPSPoint> List)
        {
            try
            {
                string points = string.Empty;
                for (int i = 0; i < List.Count; i++)
                {
                    if (i == 0)
                        points += List[i].Longitude + "," + List[i].latitude;
                    else
                        points += "|" + List[i].Longitude + "," + List[i].latitude;
                }
                return points;
            }
            catch (Exception ex)
            {
                return "";
            }
        }

        private string GetPhone(IEmployee employee)
        {
            try
            {
                List<IEmployeeContact> objs = (from c in employee.Contacts
                                               where c.ContactType != null && c.ContactType.Name == "手机"
                                               select c).ToList<IEmployeeContact>();
                if (objs != null && objs.Count > 0)
                    return objs[0].Value;
                else
                    return "";
            }
            catch (Exception ex)
            {
                return "";
            }
        }

        private int GetUnitIdByPoliceCode(string p)
        {
            string sql = "select o.Id from policeManager2.dbo.Org_Organization as o inner join [case].dbo.[user] as u on o.code = u.unitcode where u.Number = '" + p + "'";
            int organizationId = 0;
            string conStr = ConfigurationManager.AppSettings["Case"];
            try
            {
                SqlConnection con = new SqlConnection(conStr);
                SqlCommand command = new SqlCommand(sql, con);
                con.Open();
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    organizationId = Convert.ToInt32(reader[0]);
                }
            }
            catch (Exception ex)
            {

            }
            return organizationId;
        }
    }
}